#! /usr/bin/env  python

from DrupalSoap_client import *
from ZSI import FaultException
import sys

filename ="VBLMetaMan.wsdl" 
FILENAME = "https://vbl.synchrotron.org.au/MetaMan/VBLMetaMan.wsdl"
URL =  "http://cima.example.com:8080/?q=services/soap"
LOGNAME=""
PASSWORD=""

DATASETID="10tdk013"

def getFromListResponse(key,list):
    for hash in list:
       if type(hash) == type({}) and hash.has_key('title') \
              and hash['title']==key:
          return hash['value']
    return none
def mapList2Hash(list):
    hash = {}
    for dict in list:
       if type(dict) == type({}) and dict.has_key('title') \
              and dict.has_key('value'):
          hash[dict['title']] = dict['value']
       else:
          raise Exception, "trouble parsing dict %s" % dict
    return hash
    

if __name__ == '__main__':
    print "locating the service"
    loc = DrupalSoapLocator()

    # prints messages sent and received if tracefile is set
    import sys
    kw = {  #  'tracefile' : sys.stdout, 
        'user': LOGNAME, 'password':PASSWORD,
        'url': URL,
        'auth' : ( ZSI.client.AUTH.httpbasic,  LOGNAME, PASSWORD ) }

    print "binding ..."
    binding = loc.getDrupalSoapPort(**kw) # returns SOAP object

    print "\ntry to login ..."
    request = user_loginRequest()
    request._username = LOGNAME
    request._password = PASSWORD
    response = binding.user_login(request)
    resp = response._return
    print "got resp: " , type(resp)
    hash = mapList2Hash(resp)
    print "got response: " , hash


    print "\nget log Id..."
    request = proposaldb_xgetLogIdFromDatasetRequest()
    request._dataset_name=DATASETID
    try:
      response = binding.proposaldb_xgetLogIdFromDataset(request)
      print "did we get anything? "
    except FaultException, e:
      print "uh oh", e
      exit(1)

    resp = response._return
    print "got resp: " , type(resp)
    print "got resp: " , resp
    hash = mapList2Hash(resp)
    l_id = int(hash['l_id'])
    print "Log Id " , l_id


    print "\nget log record"
    request = proposaldb_xgetLogFromIdRequest()
    request._l_id = l_id
    response = binding.proposaldb_xgetLogFromId(request)
    print "did we get anything? "
    resp = response._return
    print "got resp: " , type(resp)
    print "got resp: " , resp
    hash = mapList2Hash(resp)
    print "Log  " , hash
    
    print "\nget sample Id..."
    request = proposaldb_xgetSampleFromIdRequest()
    request._s_id = int(hash['s_id'])
    response = binding.proposaldb_xgetSampleFromId(request)
    print "did we get anything? "
    resp = response._return
    print "got resp: " , type(resp)
    print "got resp: " , resp
    hash = mapList2Hash(resp)
    print "sample  " , hash

    import sys
    sys.exit()

    print "\nget scientists ..."
    import datetime
    date = list(datetime.datetime.now().timetuple())
    date[6]=0
    print date
    date = [2009, 11, 26, 15, 41, 3, 0, 330, -1]
    request = proposaldb_getAllScientistsSinceRequest()
    request._date=date
#    request._date=[2010, 11, 26, 15, 41, 3, 0, 330, -1]
    response = binding.proposaldb_getAllScientistsSince(request)
    print "did we get anything? "
    resp = response._return
    print "got resp: " , type(resp)
    hash = mapList2Hash(resp)
    print "got resp: " , hash

    print "\ntry to connect ..."
    request = system_connectRequest()
    response = binding.system_connect(request)
    print "did we connect? "
    resp = response._return
    print "got resp: " , type(resp)
    print "got resp: " , resp

   
    print "\nget scientists ..."
    request = proposaldb_getAllScientistsSinceRequest()
    import datetime
    date = list(datetime.datetime.now().timetuple())
    date[6]=0
    date = [2004, 1, 1, 0, 0, 0, 0, 330, -1]
    request._date=date
#    request._date=[1, 11, 26, 15, 41, 3, 0, 330, -1]
    print date
    response = binding.proposaldb_getAllScientistsSince(request)
    print "did we get anything? "
    resp = response._return
    print "got resp: " , type(resp)
    print "got resp: " , resp

    print "\ntry to logout ..."
    request = user_logoutRequest()
    #request._username = LOGNAME
    #request._password = PASSWORD
    response = binding.user_logout(request)
    resp = response._return
    print "got resp: " , type(resp)
    print "got response: " , resp

    print "\ngetting Prop Echo ..."
    request = proposaldb_echoRequest()
    request._message = "The bloody thing worked"
    response = binding.proposaldb_echo(request)
    resp = response._return
    print "got resp: " , type(resp)
    print "got result: " , resp
